package com.knight.admin.utils;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.log4j.Logger;

import com.knight.admin.common.constant.Constant;

public class FileUtils {
	private static final Logger logger = Logger.getLogger(FileUtils.class);

	private FileUtils() {
	}

	public static void deleteFile(String path) {
		try {
			File file = new File(Constant.UPLOAD_IMAGE_FILE_PATH + path);
			if (file.exists()) {
				file.delete();
			}
		} catch (Exception e) {
			logger.error("--------delete image fail,exception:", e);
			logger.error("--------delete image fail,imgSrc=" + path);
		}
	}

	/**
	 * 通过html字符串查找所有img的src属性
	 * 
	 * @param htmlStr
	 *            html字符串
	 * @return
	 */
	public static List<String> getImgSrc(String htmlStr) {
		String img = "";
		Pattern p_image;
		Matcher m_image;
		List<String> pics = new ArrayList<String>();
		// String regEx_img = "<img.*src=(.*?)[^>]*?>"; //图片链接地址
		String regEx_img = "<img.*src\\s*=\\s*(.*?)[^>]*?>";
		p_image = Pattern.compile(regEx_img, Pattern.CASE_INSENSITIVE);
		m_image = p_image.matcher(htmlStr);
		while (m_image.find()) {
			img = img + "," + m_image.group();
			// Pattern.compile("src=\"?(.*?)(\"|>|\\s+)").matcher(img); //匹配src
			Matcher m = Pattern.compile("src\\s*=\\s*\"?(.*?)(\"|>|\\s+)").matcher(img);
			while (m.find()) {
				pics.add(m.group(1));
			}
		}
		return pics;
	}
}
